package com.zdx.yswl.ui.tool;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by Lupengwei on 2015/8/7.
 */

public class HttpBindDeviceThread extends Thread
{
    private String userId;
    private String devId;
    private Boolean data;
    private String tag;
    private Handler handler;
    private HttpURLConnection httpURLConnection;
    private Message message;
    private Bundle bundle;
    private URL url;

    public HttpBindDeviceThread(String userId, String devId, Handler handler, String tag)
    {
        this.data = false;
        this.handler = handler;
        this.userId = userId;
        this.devId = devId;
        this.tag = tag;
    }

    @Override
    public void run()
    {
        super.run();

        message = handler.obtainMessage();
        bundle = new Bundle();

        try
        {
            url = new URL("http://m.api.lifedoo.cn/device/bind" +
                    "?userId=" + userId + "&deviceId=" + devId + "&tag=" +
                          URLEncoder.encode(tag, "utf-8"));

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setReadTimeout(5000);

            int code = httpURLConnection.getResponseCode();
            String stringJson = null;
            if(code == 200) // 服务器接收请求
            {
                stringJson = changeInputString(httpURLConnection.getInputStream());
                JSONObject jsonObject = new JSONObject(stringJson);
                data = jsonObject.getBoolean("data");
//                Log.e("data add", "" + data);
                bundle.putInt("code", code);
                bundle.putBoolean("status", data);
            }
            else
            {
                bundle.putInt("code", code);
                bundle.putBoolean("status", false);
            }
        }
        catch(IOException e)
        {
            e.printStackTrace();
            if(e instanceof SocketTimeoutException)
            {
	            bundle.putInt("code", 3001); // 连接上服务器超时的情况
            }
            else
            {
            	bundle.putInt("code", 3000);
            }
        }
        catch(JSONException e)
        {
            e.printStackTrace();
            bundle.putInt("code", 4000);
        } 
        finally
        {
            // 只是测试用的
            bundle.putString("tag", tag);

            message.setData(bundle);
            message.sendToTarget();

            httpURLConnection.disconnect();
        }
    }

    // 读取数据
    private String changeInputString(InputStream inputStream)
    {
        String jsonString="";
        ByteArrayOutputStream outPutStream=new ByteArrayOutputStream();
        byte[] data=new byte[1024];
        int len=0;
        try
        {
            while((len=inputStream.read(data))!=-1)
            {
                outPutStream.write(data, 0, len);
            }
            jsonString=new String(outPutStream.toByteArray());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return jsonString;
    }
}
